#include "processbar.h"
#include <time.h>
#include <stdlib.h>
#include <unistd.h>

//下载文件大小
#define FILESIZE 1024*1024*100

void download(callback_t cb)
{
  const unsigned FileSize = FILESIZE;
  //生成随机数种子
  srand(time(NULL)^1023);
  //剩余需要下载的量
  unsigned int total = FILESIZE;
  while(total)
  {
    //每次下载时间0.1s
    usleep(100000);
    //每次下载量1~(1024*1024-1)
    unsigned int one = rand()%(1024*1024);
    if(total > one)
      total -= one;
    else
      total = 0;
    double rate = (FileSize - total) / (double)FileSize * 100;
    cb(rate);
  }
  printf("\n");
}

int main()
{
  download(processbar);
  return 0;
}
